﻿package org.davidliebman.flash.awesomeguy {
	
	public class AGSpriteXMonster extends AGSpriteMonster {

		public function AGSpriteXMonster(mymode:AGMode, kind:int) {
			super(mymode,kind);
			// constructor code
			
			if (this.sprite_type == AGMode.S_XMONSTER_CLIMBER) {
				
				//this.myPhysics = new AGPhysicsSprite(this.myMode.myStage, this.myMode);
				
			}
		}

		public override function updateSprite():void {
			var i:int;
			var xx:int,yy:int,z:int;
			var move:int = 3 *2;//3
			var markerTest:Boolean = false;
			var hide:Boolean = true;
			var show:Boolean = false;
			var visibility:Boolean = false;
			//int index_num = 0;
			var vert:int = 32;//64;
			var horiz:int = 32;//64;
			var width:int = 2;
			
			
			
			//for each monster...
			if( this.sprite_type == AGMode.S_XMONSTER || this.sprite_type == AGMode.S_XMONSTER_STAND ){//||
			   
				
					//if (i == 5) return;
					if ( true) {
						markerTest = false;//FALSE; 
						
						if (this.active ==true  ) {
							xx = Math.floor(int ( this.x / horiz));
							yy = Math.floor(int ( this.y / vert)) ;
							
							
							// Must move and stop monsters when they hit bricks or
							// markers or the end of the screen/room/level.
							//if (xx < 0 || yy < 0 ) return;
							//if (xx > myHoriz || yy > myVert) return;
							
							if(this.facingRight == true ) {
			
								this.x = this.x + move;
								// marker test
								if (xx + 3 < myMode.myHoriz && yy + 2 < myMode.myVert || true ) {
									
									if(myMode.myInvisible[yy][xx+width] + myMode.mapcheat == AGModeGuy.B_BLOCK  ) markerTest = true;//TRUE;
									if(myMode.myInvisible[yy][xx+width] + myMode.mapcheat == AGModeGuy.B_MARKER ) markerTest = true;// TRUE;
									if(myMode.myInvisible[yy+1][xx+width]  == 0) markerTest = true;//TRUE;
								}
								// turn monster
								if (this.x > myMode.myHoriz * horiz  - (horiz * 2) || markerTest == true ) {
			
									this.facingRight=false;//FALSE;
								}
							}
							else {
			
								this.x = this.x - move;
								// marker test
								if (xx -1 > 0 && yy + 2 < myMode.myVert || true) {
									if(myMode.myInvisible[yy][xx] + myMode.mapcheat == AGModeGuy.B_BLOCK) markerTest = true;//TRUE;
									if(myMode.myInvisible[yy][xx] + myMode.mapcheat == AGModeGuy.B_MARKER) markerTest =true;// TRUE;
									if(myMode.myInvisible[yy+1][xx-1] == 0) markerTest = true;//TRUE;
								}
								// turn monster
								if (this.x < 0 || markerTest == true ) {
			
									this.facingRight= true;//TRUE;
								}
							}
			
							//Only show monsters that are on the screen properly
			
			
							//default is to show monster
							visibility = show;
							//hide monster if...
							if(this.x > myMode.scrollBGX + 64 * horiz + 32 ) {
								visibility = hide;
							}
							if (this.x < myMode.scrollBGX - 32) {
								visibility = hide;
							}
							if (this.y > myMode.scrollBGY + 48 * vert + 32) {
								visibility = hide;
							}
							if ( this.y < myMode.scrollBGY  - 32) {
								visibility = hide;
							}
						}
						//visibility = show;
						//swap monsters
						if (this.visible && visibility == show) this.visible = true;// TRUE;
						
						
					}
					
				
		
			}
			else if (this.sprite_type == AGMode.S_XMONSTER_CLIMBER) {
				//this.visible = true;
				
				this.makeRails();
				this.addRails(this.myMode.myStage);
				
				if (this.bitmap == null) return;
				
				this.ai.setStartEnd(this.x, this.y, this.myMode.xpos, this.myMode.ypos);//, 0, null);
				
				var somex:int = this.ai.getPixHintX();
				var somey:int = this.ai.getPixHintY();
										
				this.myPhysics.applyGravityAndLadders(this.myMode.myInvisible, this, somex,somey); 
											//this.ai.getPixHintX(), this.ai.getPixHintY());
				this.ai.drawMap();
				
				//this.x = this.bitmap.x;
				//this.y = this.bitmap.y;
				
			}
			
			myMode.drawRadarPing(myMode.radar, 
								 myMode.radarscreen, 
								 this.x, this.y, 
								 AGMode.PING_OTHER, 
								 0xff00ff00);
			
			return;
		}

	}
	
}
